// Set up jQuery to only accept javascript, so we don't have to append .js to
// every AJAX request URL. Without this Rails will interpret the AJAX request as
// an normal HTTP GET request.
jQuery.ajaxSetup({
  'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); }
})

// A helper method for submitting a form with an AJAX request. The returned
// javascript is then evaluated in client-side context.
jQuery.fn.submitWithAjax = function() {
  this.live('submit', function() {
    jQuery.post(this.action, jQuery(this).serialize(), null, "script");
    return false;
  })
  return this;
};

// A helper method for overriding link click behavior with an AJAX request. For
// example, overriding links with class "foo" with $(".foo").getWithAjax();
// would unobtrusively override their click behavior to make an AJAX-request
// instead. The contents of the #contents div are replaced with the returned
// html.
jQuery.fn.replaceContentWithAjax = function() {
  this.live('click', function() {
    jQuery('body > .qtip').remove();
    jQuery("#content").load(this.href);
    return false;
  });
  return this;
};

jQuery.fn.progressBarify = function() {
  this.each(function() {
    jQuery(this).progressBar({
      steps: 1,
      showText: false,
      textFormat: 'percentage',
      width: 60,
      boxImage: '../../images/progressbar.gif',
      barImage: {
        0:  '../../images/progressbg_red.gif',
        25: '../../images/progressbg_orange.gif',
        50: '../../images/progressbg_yellow.gif',
        75: '../../images/progressbg_green.gif'
      }
    });
  });
}

jQuery.fn.hideTooltipsOnClick = function() {
  this.live('click', function() {
    jQuery("body > .qtip").hide();
  });
  return this;
};

jQuery.fn.imageZoom = function(zoomLevel) {
  this.live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      var imageBox = jQuery('<div>').css({
        display:  'none',
        position: 'absolute',
      }).addClass('zoomed-image').offset({
        top:      jQuery(this).offset().top  - jQuery(this).height(),
        left:     jQuery(this).offset().left + jQuery(this).width() + 32
      });

      var newImage = jQuery(this).clone(false).removeClass().unbind().css({
        width:  jQuery(this).width()  * zoomLevel,
        height: jQuery(this).height() * zoomLevel
      });

      imageBox.html(newImage);
      jQuery('body').append(imageBox);
      imageBox.delay('fast').fadeIn('fast');
    } else if (event.type == 'mouseout') {
      jQuery('body > .zoomed-image').fadeOut('fast', function() { jQuery(this).remove(); });
    }
  });
};

flash = function(place, message) {
  $("#notifications").append($('<div style="display:none"></div>')
                             .attr('id', place).addClass('notification').html(message).fadeIn());
  hideNotifications();
};

hideNotifications = function() {
  $("#notifications > div").delay(5000).fadeOut("slow");
}

$(document).ready(function() {
  $("a").hideTooltipsOnClick();

  $("[title]").live('mouseover', function() {
    $(this).qtip({
      overwrite: false,
      show: { ready: true },
      style: { name: 'cream' },
      position: { adjust: { screen: true } }
    });
  });

  $('.fragment-thumb').imageZoom(2.25);

  hideNotifications();
  $(".job-input-method a").replaceContentWithAjax();
});
